package com.cjy.user.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.persistent.model.Terminal;
import com.base.common.utils.EasyUtil;
import com.cjy.common.bean.OpAttributes;
import com.cjy.user.service.UserService;

@Action("userAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class UserAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private UserService userService;

	private String code;

	private String openId;

	private String phone;

	private int captcha;

	private int bid = 0; // 0,android城际云; 1,android城管家; 2,微信城际云; 3,ios城际云;
							// 4,ios云管家

	private int userChooseCompoundsId;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public int getBid() {
		return bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public int getCaptcha() {
		return captcha;
	}

	public void setCaptcha(int captcha) {
		this.captcha = captcha;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public int getUserChooseCompoundsId() {
		return userChooseCompoundsId;
	}

	public void setUserChooseCompoundsId(int userChooseCompoundsId) {
		this.userChooseCompoundsId = userChooseCompoundsId;
	}

	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	/**
	 * 通知服务器准备注册
	 * 
	 * @return
	 */
	public String prepareRegiste() {
		try {
			Terminal terminal = new Terminal();
			terminal.setBid(bid);
			terminal.setUserChooseCompoundsId(userChooseCompoundsId);
			int result = 0;

			// 判断注册设备类型
			if (bid == 2) {
				if ("-1".equals(openId.trim())) {
					throw new Exception();
				}
				terminal.setOpenId(openId);
				terminal.setPhone(phone.trim());
				result = userService.weChatGetCaptcha(terminal);
			} else if (bid == 3) {
				terminal.setAppleId(code);
				terminal.setPhone(phone.trim());
				result = userService.iosGetCaptcha(terminal);
			} else {
				terminal.setCode(code);
				terminal.setPhone(EasyUtil.rc4(phone, "szcjy").trim());
				result = userService.androidGetCaptcha(terminal);
			}

			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", result));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 城际云注册
	 * 
	 * @return
	 */
	public String registe() {

		try {
			Terminal terminal = new Terminal();
			// 判断是否是哪种设备注册
			if (bid == 2) {
				terminal.setOpenId(openId);
				terminal.setPhone(phone.trim());
			} else if (bid == 3) {
				terminal.setAppleId(code);
				terminal.setPhone(phone.trim());
			} else {
				terminal.setCode(code);
				terminal.setPhone(EasyUtil.rc4(phone, "szcjy").trim());
			}
			terminal.setBid(bid);
			terminal.setCaptcha(captcha);
			int code = 0;
			if (bid == 1) {
				code = userService.managerRegiste(terminal);
			} else {
				code = userService.registe(terminal);
			}
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", code));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 城际云登录
	 * 
	 * @return
	 */
	public String login() {
		try {
			Terminal terminal = new Terminal();

			if (bid == 2) {
				terminal.setOpenId(openId.trim());
			} else if (bid == 3) {
				terminal.setAppleId(code.trim());
			} else {
				terminal.setCode(code.trim());
			}
			terminal.setBid(bid);
			List users = userService.login(terminal);
			session.put("users", users);
			if (users.size() > 0) {
				actionResult = ActionResult.getActionResult(new OpAttributes()
						.add("code", 1).add("result", users));
			} else {
				actionResult = ActionResult.getActionResult(new OpAttributes()
						.add("code", 0));
			}
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

}
